package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Page<SetmealDto> pageByName(int page, int pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
        return setmealMapper.pageByName(pageInfo, name);
    }

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 添加套餐 并添加套餐菜品
     *
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //添加套餐
        save(setmealDto);
        //添加套餐菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //设置套餐菜品表的套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐 并删除对应的套餐菜品表
     * 起售状态不允许删
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
        setmealLqw.eq(Setmeal::getStatus,1);
        setmealLqw.in(Setmeal::getId,ids);
        int count = count(setmealLqw);
        if (count>0){
            throw new CustomException("存在启售套餐,不允许删除");
        }
        //删除套餐基本信息
        removeByIds(ids);

        //条件构造器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        //删除对应的套餐菜品信息
        setmealDishService.remove(lqw);
    }

    /**
     * 根据id查询套餐信息及套餐菜品表
     *
     * @param id
     */
    @Override
    public SetmealDto getByIdWithDishes(Long id) {
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐信息及套餐菜品
     *
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDishes(SetmealDto setmealDto) {
        //修改套餐基本信息
        updateById(setmealDto);

        //删除原有的套餐菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);
        //新增套餐菜品
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        //设置setmealId
        list.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(list);
    }
}
